﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mNC.Calculate
{
    public class Cal_TotalOutPut
    {
        //获取扫描加工当天完成工时
        public static string GetTotalWorkHours()
        {
            string result = "";
            int workHours = 0;
            List<string> list = new List<string>();
            StringBuilder s1 = new StringBuilder();
            DataSet ds = new DataSet();
            //获取当天扫描表记录
            DataTable dt = Common.GetDataTable.Get_RecordToday();
            //生成扫描记录临时表
            DataTable dtTemp = Common.GetTable.GetNCProcess_Record();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string[] str = dt.Rows[i]["BARCODE_NO"].ToString().Split('_');
                    list.Add(str[1]);
                    DataRow dr = dtTemp.NewRow();
                    dr["PART_NO"] = str[1];
                    dr["PROCESS_QTY"] = dt.Rows[i]["PROCESS_QTY"].ToString();
                    dtTemp.Rows.Add(dr);

                }
            }
            //取得当天部件号的所有工时
            s1.AppendFormat(@" AND PART_NO  IN ('{0}')", string.Join("','", list.Distinct().ToArray()));
            string sql = @"SELECT PART_NO,SUM(PROCESS_TIME) as PROCESS_TIME
         FROM [LamexMESManager].[dbo].[IE_Part_ProcessTime] 
         where PROCESS_CODE='P05' AND EXPIRY_DATE='9999-12-31' " + s1 + " group by PART_NO";
            DataTable dataTable = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            if (dataTable.Rows.Count > 0 && dataTable != null)
            {
                //dataTable表左连接dtTemp表，数量*工时
                //采用Datatable.Copy()出一个新的表，避免出现Daset重复写入
                DataTable dt1 = dataTable.Copy();
                dt1.TableName = "dt1n";
                DataTable dt2 = dtTemp.Copy();
                dt2.TableName = "dt2n";
                ds.Tables.Add(dt1);
                ds.Tables.Add(dt2);
                ds.Relations.Add("LeftJoin", ds.Tables["dt1n"] .Columns["PART_NO"], ds.Tables["dt2n"].Columns["PART_NO"], false);
                foreach (DataRow parentRow in ds.Relations["LeftJoin"].ParentTable.Rows)
                {
                    foreach (DataRow childRow in parentRow.GetChildRows(ds.Relations["LeftJoin"]))
                    {
                        workHours += Convert.ToInt32(parentRow["PROCESS_TIME"].ToString()) *
                            Convert.ToInt32(childRow["PROCESS_QTY"].ToString());
                    }
                }
                // workHours = Convert.ToInt32(dataTable.Rows[0]["PROCESS_TIME"].ToString());
                TimeSpan ts = new TimeSpan(0, 0, workHours);
                result = ts.Hours + "小时" + ts.Minutes + "分" + ts.Seconds + "秒";

            }
            return result;
        }

        //获取当前跟进表累计欠货数量
        public static double GetNoFinishQty(string OrderNumber)
        {
            double result = 0, result_all = 0, result_temp = 0;
            //取得当天表【Transfer_Part_Detailed】的数量
            string sql = @" select sum(de.Part_QTY) Part_QTY 
         FROM [LamexMESManager].[dbo].[Transfer_Part_Detailed] DE
         JOIN [LamexMESManager].[dbo].[IE_Part_Routing] RO on RO.Part_No=DE.Part_No
         JOIN [LamexMESManager].[dbo].[IE_Routing_Combination] CO ON RO.GROUP_CODE=CO.GROUP_CODE AND CO.ROUTING_CODE like '%P05%' 
         where DE.Product_TS<=CONVERT(varchar,GETDATE(),23) ";
            DataTable dataTable = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            if (dataTable.Rows.Count > 0)
            {
                if (!string.IsNullOrEmpty(dataTable.Rows[0]["Part_QTY"].ToString()))
                {
                    result_all = Convert.ToDouble(dataTable.Rows[0]["Part_QTY"].ToString());
                }
            }
            //取得扫描记录表的数量
            string sql1 = @" SELECT SUM(PROCESS_QTY) as PROCESS_QTY
         FROM [LamexMESManager].[dbo].[NC_PN_AL_Manufacture_Process_Record]
         WHERE BARCODE_NO like '" + OrderNumber + "%'";
            DataTable dataTable1 = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql1);
            if (dataTable1.Rows.Count > 0)
            {
                if (!string.IsNullOrEmpty(dataTable1.Rows[0]["PROCESS_QTY"].ToString()))
                {
                    result_temp = Convert.ToDouble(dataTable1.Rows[0]["PROCESS_QTY"].ToString());
                }
            }
            result = result_all - result_temp;
            return result;
        }
    }
}
